﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Linq.Expressions;

namespace PH.Data
{
    public class DBCentre : IDisposable
    {
        PhysicalHealthEntities _dataContext;

        public DBCentre()
        {
            _dataContext = new PhysicalHealthEntities();
        }

        public PhysicalHealthEntities DataContext
        {
            get
            {
                return _dataContext;
            }
        }

        public void Dispose()
        {
            if (_dataContext != null)
            {
                try
                {
                    _dataContext.Dispose();
                }
                catch { }
            }
        }

        public void Delete<T>(IQueryable<T> list) where T : class
        {
            try
            {
                foreach (T item in list)
                {
                    DataContext.Set<T>().Remove(item);
                }
                DataContext.SaveChanges();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        public void Delete<T>(ICollection<T> list) where T : class
        {
            try
            {
                while (list.Count() > 0)
                {
                    var item = list.ElementAt<T>(0);
                    DataContext.Set<T>().Remove(item);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        public void Delete<T>(Expression<Func<T, bool>> predicate) where T : class
        {
            Delete<T>(DataContext.Set<T>().Where(predicate));
        }

        public void Insert<T>(ICollection<T> lst) where T : class
        {
            foreach (var t in lst)
                DataContext.Set<T>().Add(t);
        }
    }
}
